﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using Dtf.Core.Remoting;
using ResourceManager;

namespace Dtf.Core.Resources
{
    public class Win32DialogItemProvider : ResourceProvider
    {
        /*
        bool EnumResNameProc(IntPtr hModule, IntPtr lpszType, IntPtr lpszName, IntPtr lParam)
        {
            if (lpszName.ToString().CompareTo(_strDlgID) != 0)
            {
                return true;
            }

            bool bIsDlgEx;
            short nControlCount;
            IntPtr hResInfo = NativeMethods.FindResource(hModule, lpszName, lpszType);
            IntPtr mem = NativeMethods.LoadResource(hModule, hResInfo);
            IntPtr pb = NativeMethods.LockResource(mem);
            IntPtr pRes = pb; //use for DWORD alignment
            DLGTEMPLATEEX dlgEx = new DLGTEMPLATEEX();
            DLGTEMPLATE dlg = new DLGTEMPLATE();
            Marshal.PtrToStructure(pb, dlgEx);
            if ((dlgEx.wSignature & 0xFFFF) == 0xFFFF)    //ex dialog
            {
                nControlCount = dlgEx.cDlgItems;
                bIsDlgEx = true;
            }
            else
            {

                Marshal.PtrToStructure(pb, dlg);
                nControlCount = dlg.cdit;
                bIsDlgEx = false;

            }




            int dlgtmp_size = Marshal.SizeOf(dlgEx);
            if (!bIsDlgEx)
            {
                dlgtmp_size = Marshal.SizeOf(dlg);
            }
            pb = (IntPtr)(pb.ToInt64() + dlgtmp_size);


            //DLGITEMTEMPLATEEX dlgItmEx2 = new DLGITEMTEMPLATEEX();
            //Marshal.PtrToStructure(pb, dlgItmEx2);
            //string resID2 = Convert.ToString(dlgItmEx2.id);



            short t = Marshal.ReadInt16(pb);    //menu
            if (t == 0x0000)
            {
                pb = (IntPtr)(pb.ToInt64() + 2);

            }
            else if ((t & 0xFFFF) == 0xFFFF)
            {
                pb = (IntPtr)(pb.ToInt64() + 4);
            }
            else
            {
                string tID = Marshal.PtrToStringUni(pb);
                pb = (IntPtr)(pb.ToInt64() + tID.Length * sizeof(char) + 2); //null terminated
            }

            t = Marshal.ReadInt16(pb);  //windowclass
            if (t == 0x0000)
            {
                pb = (IntPtr)(pb.ToInt64() + 2);

            }
            else if ((t & 0xFFFF) == 0xFFFF)
            {
                pb = (IntPtr)(pb.ToInt64() + 4);
            }
            else
            {
                string tID = Marshal.PtrToStringUni(pb);
                pb = (IntPtr)(pb.ToInt64() + tID.Length * sizeof(char) + 2);
            }


            t = Marshal.ReadInt16(pb);  //title
            if (t != 0x0000)
            {
                pb = (IntPtr)(pb.ToInt64() + Marshal.PtrToStringUni(pb).Length * sizeof(char) + 2);


            }
            else
            {
                pb = (IntPtr)(pb.ToInt64() + 2);
            }




            bool bDS_SETFONT;
            if (bIsDlgEx)
            {
                bDS_SETFONT = (dlgEx.dwStyle & 0x40L) == 0x40L;
                bool bShellFont = (dlgEx.dwStyle & (0x40L | 0x8L)) == (0x40L | 0x8L);

                if (bDS_SETFONT || bShellFont)
                {
                    pb = (IntPtr)(pb.ToInt64() + 2 + 2 + 1 + 1); //pointsize, weight, italic, charset
                    pb = (IntPtr)(pb.ToInt64() + Marshal.PtrToStringUni(pb).Length * sizeof(char) + 2); //typeface
                }

            }
            else
            {
                bDS_SETFONT = (dlg.style & 0x40L) == 0x40L;
                if (bDS_SETFONT)
                {
                    pb = (IntPtr)(pb.ToInt64() + 2);
                    pb = (IntPtr)(pb.ToInt64() + Marshal.PtrToStringUni(pb).Length * sizeof(char) + 2);
                }

            }



            // pb = (IntPtr)(pb.ToInt64() + 4 - (pb.ToInt64() - pRes.ToInt64() - (pb.ToInt64() - pRes.ToInt64()) / 4 * 4)); //DWORD alignment

            for (int i = 0; i < nControlCount; i++)
            {
                pb = (IntPtr)(((pb.ToInt64() + 3L) / 4L) * 4L);     //DWORD alignment

                string resID;
                if (bIsDlgEx)
                {
                    DLGITEMTEMPLATEEX dlgItmEx = new DLGITEMTEMPLATEEX();

                    Marshal.PtrToStructure(pb, dlgItmEx);
                    resID = Convert.ToString(dlgItmEx.id);
                    pb = (IntPtr)(pb.ToInt64() + Marshal.SizeOf(dlgItmEx));
                }
                else
                {
                    DLGITEMTEMPLATE dlgItm = new DLGITEMTEMPLATE();

                    Marshal.PtrToStructure(pb, dlgItm);
                    resID = Convert.ToString(dlgItm.id);
                    pb = (IntPtr)(pb.ToInt64() + Marshal.SizeOf(dlgItm));
                }


                short s = Marshal.ReadInt16(pb); //windowClass
                if ((s & 0xFFFF) != 0xFFFF) //null terminated string
                {
                    pb = (IntPtr)(pb.ToInt64() + Marshal.PtrToStringUni(pb).Length * sizeof(char) + 2);
                }
                else    //0xFFFF atom values
                {
                    pb = (IntPtr)(pb.ToInt64() + 4);
                }

                s = Marshal.ReadInt16(pb); //title
                if ((s & 0xFFFF) != 0xFFFF) //null terminated string
                {
                    if (resID.CompareTo(_strCtrlId) == 0)
                    {
                        _strCtrlName = Marshal.PtrToStringUni(pb);
                        return false;
                    }

                    pb = (IntPtr)(pb.ToInt64() + Marshal.PtrToStringUni(pb).Length * sizeof(char) + 2);

                }
                else    //0xFFFF 4bit
                {
                    pb = (IntPtr)(pb.ToInt64() + 4);    //2+2(title)
                }

                s = Marshal.ReadInt16(pb);  //other data

                pb = (IntPtr)(pb.ToInt64() + 2);  //extraCount 2bytes
                pb = (IntPtr)(pb.ToInt64() + s);  //extra data





            }
            return true;
        }
         */


        public Win32DialogItemProvider(string resourceTypeName)
            : base(resourceTypeName)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="resourceKey">;My account;Win32DialogItemString;LCLang.dll;201;1390</param>
        /// <returns></returns>
        public override string ExtractString(string filename, string[] tokens)
        {

            if (tokens==null || tokens.Length != 2)
            {
                throw new ResourceKeyException();
            }

            return new ResourceFetcher().GetDialogItemNameFromResources(null, filename, tokens[0], tokens[1]);
            
            
        }

       
    }
}
